<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *   Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>  
 */
 
require_once("entity.class.php");

class glossary extends entity {

	var $id;
	var $eterm;
	var $translation;
	var $context;
	var $related;
	var $comments;
	var $lastupdate;
	var $teamid;
	var $userid;
	
	var $translationlist;
	var $relatedlist;
	
    public function __construct()
    {
    }

	// THIS CREATE A NEW INSTANCE OF THIS CLASS
	public function newInstance() {
		return new glossary;
	}

	// COMMON PUBLIC FUNCTIONS // NEEDED FOR LOADER
	public function get() {
		$sql = "SELECT * FROM ".GLOSSARY." WHERE id = ".util::str($this->id);
		return $sql;
	}

	public function findByTerm() {
		$sql = "SELECT * FROM ".GLOSSARY." WHERE english_term = ".util::str($this->eterm). " AND teamid = ".util::str($this->teamid);
		return $sql;
	}
	
	public function listAll() {
		$sql = "SELECT * FROM ".GLOSSARY." WHERE teamid = ".util::str($this->teamid)." order by english_term";
		return $sql;
	}

	public function listStartsWith($w) {
		$sql = "SELECT * FROM ".GLOSSARY." WHERE teamid = ".util::str($this->teamid)." AND left(english_term,".strlen($w).")=".util::str($w)." order by english_term";
		return $sql;
	}
	
	public function find($w) {
		$sql = "SELECT * FROM ".GLOSSARY." WHERE MATCH(english_term,translation,context) AGAINST(".util::str($w)." IN BOOLEAN MODE) AND teamid = ".util::str($this->teamid);
		return $sql;		
	}
	
	public function delete() {
		$sql = "DELETE FROM ".GLOSSARY;
		$sql .= " WHERE english_term = ".util::str($this->eterm);
		$sql .= " AND teamid = ".util::str($this->teamid);
		return $sql;
	}
	
	public function insert() {
		$now = util::now();
      	$sql = "INSERT INTO ".GLOSSARY." (english_term, translation, context, related, comments, lastupdate, lastuserid, teamid) ";
      	$sql .= "VALUES (";
      	$sql .= util::str($this->eterm).",";
      	$sql .= util::str($this->translation).",";
      	$sql .= util::str(util::encode($this->context)).",";
      	$sql .= util::str($this->related).",";
      	$sql .= util::str(util::encode($this->comments)).",";
      	$sql .= util::str($now).",";
      	$sql .= util::str($this->userid).",";
      	$sql .= util::str($this->teamid).")";
		return $sql;
	}

	public function update() {
		$now = util::now();
      	$sql = "UPDATE ".GLOSSARY." SET ";
      	$sql .= " english_term = ".util::str($this->eterm).",";
      	$sql .= " translation = ".util::str(util::encode($this->translation)).",";
      	$sql .= " context = ".util::str(util::encode($this->context)).",";
      	$sql .= " related = ".util::str($this->related).",";
      	$sql .= " comments = ".util::str(util::encode($this->comments)).",";
      	$sql .= " lastupdate = ".util::str($now).",";
      	$sql .= " lastuserid = ".util::str($this->userid).",";
      	$sql .= " teamid = ".util::str($this->teamid);
      	$sql .= " WHERE id = ".util::str($this->id);
		return $sql;
	}
	
	public function fill($array) {
		$this->id=$array['id'];
		$this->eterm=$array['english_term'];
		$this->translation=util::decode($array['translation']);
		$this->context=util::decode($array['context']);		
		$this->related=$array['related'];
		$this->comments=util::decode($array['comments']);
		$this->lastupdate=$array['lastupdate'];
		$this->userid=$array['lastuserid'];
		$this->teamid=$array['teamid'];
		$this->translationlist = util::stringToArray($this->translation,",");
		$this->relatedlist = util::stringToArray($this->related,",");
	}

	public function formateddate($zone) {
		return util::fromUTC($this->lastupdate,$zone);	
	}
}

?>